//package com.coldchain.order.service.impl;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//
//import org.apache.ibatis.builder.BuilderException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.coldchain.cys.entity.Car;
//import com.coldchain.common.feign.CarDriverFeign;
//import com.coldchain.common.feign.CarFeign;
//import com.coldchain.message.MessageUtils;
//import com.coldchain.message.core.entity.order.CompleteMsg;
//import com.coldchain.message.core.entity.order.DeliveryMsg;
//import com.coldchain.message.core.entity.order.NewsMsg;
//import com.coldchain.order.entity.BizDispatchingOrdercao;
//import com.coldchain.order.entity.BizReceiverGoods;
//import com.coldchain.order.entity.BizReceiverPerson;
//import com.coldchain.order.entity.Order;
//import com.coldchain.order.mapper.BizDispatchingOrderMappercao;
//import com.coldchain.order.mapper.BizReceiverGoodsMapper;
//import com.coldchain.order.mapper.BizReceiverPersonMapper;
//import com.coldchain.order.mapper.OrderMapper;
//import com.coldchain.order.service.IBizReceiverPersonService;
//import BusinessException;
//import BaseResponse;
//import ObjectRestResponse;
//import FunctionUtils;
//import ValidateUtil;
//import com.function.common.GetRest;
//
///**
// * <p>
// * 订单收货员信息 服务实现类
// * </p>
// * @author fangliai
// * @since 2018-09-11
// */
//@Service
//@SuppressWarnings({ "unchecked", "rawtypes" })
//public class BizReceiverPersonServiceImpl extends ServiceImpl<BizReceiverPersonMapper, BizReceiverPerson> implements IBizReceiverPersonService {
//	@Autowired
//	private BizReceiverGoodsMapper bizReceiverGoodsMapper;
//	@Autowired
//	private BizReceiverPersonMapper bizReceiverPersonMapper;
//	@Autowired
//	private MessageUtils messageUtils;
//	@Autowired
//	private OrderMapper orderMapper;
//	@Autowired
//	private CarFeign carFeign;
//	@Autowired
//	private CarDriverFeign carDriverFeign;
//	@Autowired
//	private BizDispatchingOrderMappercao bizDispatchingOrderMapper;
//	@Override
//	public List<BizReceiverPerson> findByBizId(String bizId,String orderId) {
//		 QueryWrapper<BizReceiverPerson> ew = new QueryWrapper<BizReceiverPerson>();
//		 if(bizId!=null||"".equals(bizId)){
//			 ew.eq("biz_id={0}",bizId);
//		 }else if(orderId!=null||"".equals(orderId)){
//			 ew.eq("order_id={0}",bizId);
//		 }else{
//			 return new ArrayList<>();
//		 }
//		 List<BizReceiverPerson> list = super.list(ew);
//		 for(BizReceiverPerson b:list){
//			 QueryWrapper<BizReceiverGoods> goods = new QueryWrapper<BizReceiverGoods>();
//			 goods.eq("receiverid={0}",b.getId());
//			 b.setGoods(bizReceiverGoodsMapper.selectList(goods));
//		 }
//		return list;
//	}
//
//	@Override
//	public ObjectRestResponse<Object> sendArrive(Integer sort,String bizId, String endLogUrl, String goodsLogUrl, String resultLogUrl,Integer carrierNum,String carrierName) {
//		QueryWrapper<BizReceiverPerson> queryWrapper = new QueryWrapper<BizReceiverPerson>();
//		queryWrapper.eq("biz_id", bizId);
//		queryWrapper.eq("sort", sort);
//		BizReceiverPerson receiver = super.getOne(queryWrapper);
//		if(ValidateUtil.Objectempty(receiver)){
//			throw new BuilderException("未找到收货点记录");
//		}
//		receiver.setEndLogUrl(endLogUrl);
//		receiver.setGoodsLogUrl(goodsLogUrl);
//		receiver.setResultLogUrl(resultLogUrl);
//		receiver.setCarrierNum(carrierNum);
//		receiver.setStatus(2);
//		receiver.setCarrierName(carrierName);
//		if(this.updateById(receiver)){
//			// 进行推送消息
//			Integer sortMax = bizReceiverPersonMapper.getOrderIdSortMax(receiver.getOrderId());
//			if(ValidateUtil.isEquals(sortMax, receiver.getSort())){//最后一个配送点到达
//				BizDispatchingOrdercao bizorder = this.bizDispatchingOrderMapper.selectById(receiver.getBizId());
//				bizorder.setStatus(4);
//				int count = this.bizDispatchingOrderMapper.updateById(bizorder);
//				if(count<1){
//					return FunctionUtils.getFailRest("内部错误请重试");
//				}
//				ObjectRestResponse<Car> response = carFeign.getCarInfo(bizorder.getCarId());
//				if(response.getStatus()!=200){
//					throw new BusinessException(response.getMessage());
//				}
//				Car car = response.getData();
//				BaseResponse baseResponse = this.carFeign.doChangeCarWorkstatus(car.getId(),0);
//				if(baseResponse.getStatus()!=200){
//					throw new BusinessException(baseResponse.getMessage());
//				}
//				baseResponse = this.carDriverFeign.doIncrementSuccessOrder(car.getDriverid());
//				if(baseResponse.getStatus()!=200){
//					throw new BusinessException(baseResponse.getMessage());
//				}
//				//进行消息推送
//				DeliveryMsg deliveryMsg = new DeliveryMsg(receiver.getOrderId(),receiver.getLongitude()+"",receiver.getLatitude()+"");
//				messageUtils.sendOrderMsg(deliveryMsg);
//				sort = -1;
//			}else{
//				QueryWrapper<BizReceiverPerson> wrapper = new QueryWrapper<BizReceiverPerson>();
//				queryWrapper.eq("biz_id", bizId);
//				queryWrapper.gt("sort",sort);
//				queryWrapper.orderByAsc("sort");
//				Integer conut = this.bizReceiverPersonMapper.selectCount(wrapper);
//				if(conut!=null&&conut>0){
//					sort++;
//				}
//			/*	BizDispatchingOrdercao bizorder = this.bizDispatchingOrderMapper.selectById(receiver.getBizId());
//				QueryWrapper<BizReceiverPerson> wrapper = new QueryWrapper<>();
//				wrapper.eq("biz_id", bizorder.getBizId());
//				wrapper.eq("static",1);
//				wrapper.orderByAsc("sort");
//				List<BizReceiverPerson> personAll = this.bizReceiverPersonMapper.selectList(wrapper);
//				if(personAll!=null&&personAll.size()>0){
//					map.put("longitude", personAll.get(0).getLongitude());
//					map.put("latitude", personAll.get(0).getLatitude());
//					QueryWrapper<BizReceiverGoods> goods = new QueryWrapper<>();
//					goods.eq("receiverid", personAll.get(0).getPersonId());
//					map.put("goods", this.bizReceiverGoodsMapper.selectList(goods));
//					map.put("address",personAll.get(0).getLocation()+" "+personAll.get(0).getAddrdetail());
//					map.put("phone", personAll.get(0).getReceiverPhone());
//					map.put("name", personAll.get(0).getReceiverName());
//				}
//				*/
//				Order order = this.orderMapper.selectById(receiver.getOrderId());
//				CompleteMsg completeMsg = new CompleteMsg(receiver.getOrderId(), receiver.getLongitude()+"",receiver.getLatitude()+"",order.getCysId()+"",order.getCompanyId());
//				messageUtils.sendOrderMsg(completeMsg);
//			}
//			return FunctionUtils.getSuccessRest("已确认到达",sort);
//		}
//		throw new BuilderException("确认到达失败");
//
//	}
//
//	@Override
//	public ObjectRestResponse sendError(Integer receiverId,String errorLogUrl, String errorMsg, Integer errorCode) {
//		BizReceiverPerson receiver = super.getById(receiverId);
//		if(ValidateUtil.Objectempty(receiver)){
//			return FunctionUtils.getFailRest("未找到收货点记录");
//		}
//		receiver.setErrorLogUrl(errorLogUrl);
//		receiver.setErrorMsg(errorMsg);
//		receiver.setErrorCode(errorCode);
//		receiver.setStatus(-1);
//		//如果是最后一个配送点
//		Integer sort = bizReceiverPersonMapper.getOrderIdSortMax(receiver.getOrderId());
//		if(ValidateUtil.isEquals(sort, receiver.getSort())){//最后一个配送点到达
//			BizDispatchingOrdercao bizorder = this.bizDispatchingOrderMapper.selectById(receiver.getBizId());
//			bizorder.setStatus(4);
//			int count = this.bizDispatchingOrderMapper.updateById(bizorder);
//			if(count<1){
//				return FunctionUtils.getFailRest("内部错误请重试");
//			}
//			ObjectRestResponse<Car> response = carFeign.getCarInfo(bizorder.getCarId());
//			if(response.getStatus()!=200){
//				throw new BusinessException(response.getMessage());
//			}
//			Car car = response.getData();
//			BaseResponse baseResponse = this.carFeign.doChangeCarWorkstatus(car.getId(),0);
//			if(baseResponse.getStatus()!=200){
//				throw new BusinessException(baseResponse.getMessage());
//			}
//			baseResponse = this.carDriverFeign.doIncrementSuccessOrder(car.getDriverid());
//			if(baseResponse.getStatus()!=200){
//				throw new BusinessException(baseResponse.getMessage());
//			}
//			//进行消息推送
//			DeliveryMsg deliveryMsg = new DeliveryMsg(receiver.getOrderId(),receiver.getLongitude()+"",receiver.getLatitude()+"");
//			messageUtils.sendOrderMsg(deliveryMsg);
//		}
//		boolean falg = super.updateById(receiver);
//		if(falg){
//			return FunctionUtils.getSuccessRest("你已提交异常");
//		}
//		throw new BuilderException("提交异常失败，请重试");
//
//	}
//
//}
